<?php

namespace app\common\model\mysql;

use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class House extends BaseModel
{
    protected $schema=[
        'id'=>'int',
        'pid'=>'int',
        'name'=>'varchar',
        'sort'=>'int',
        'longitude'=>'decimal',
        'latitude'=>'decimal',
        'status'=>'int',
        'create_time'=>'int',
        'update_time'=>'int',
    ];

    /**
     * 查询配置树
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAllTree(): array
    {
        $data = $this->select();
        return $data->isEmpty()?[]:$data->toArray();
    }

    /**
     * 根据配置名称查询ID
     * @param $name
     * @return SystemCategory|array|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPidByName($name){
        $where=[
            ['name','=',$name],
            ['status','=',config("status.mysql.table_normal")],
            ['pid','=',0],
        ];
        return $this->where($where)->field('id')->find();
    }

    /**
     * 根据配置名称查询
     * @param $name
     * @return SystemCategory|array|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCategoryByName($name){
        $where=[
            ['name','in',$name],
            ['status','=',config("status.mysql.table_normal")],
            ['pid','=',0],
        ];
        return $this->where($where)->field('id,name')->select();
    }

    /**
     * 根据父级ID查询子分类
     * @param $pid
     * @return SystemCategory[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCategoryByPid($pid){
        $where=[
            ['pid','=',$pid],
            ['status','=',config("status.mysql.table_normal")],
        ];
        return $this->where($where)->field('id,name,url')->order('sort desc')->select();
    }

    /**
     * 根据父级ID查询子分类 多个
     * @param $pid
     * @return SystemCategory[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCategoryMultipleByPid($pid){
        $where=[
            ['pid','in',$pid],
            ['status','=',config("status.mysql.table_normal")],
        ];
        return $this->where($where)->field('id,name,pid')->order('sort desc')->select();
    }

    /**
     * 查询字典
     * @return SystemCategory[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getDictionaries(){
        $where=[
            ['pid','<>',0],
            ['status','=',config("status.mysql.table_normal")],
        ];
        return $this->where($where)->field('id,name')->select();
    }

    /**
     * 根据名称获取配置
     * @param $name
     * @return SystemCategory|array|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCategoryIdByName($name){
        $where=[
            ['name','=',$name],
            ['status','=',config("status.mysql.table_normal")]
        ];
        $data=$this->where($where)->field('id')->find();
        return empty($data)?null:$data['id'];
    }
}
